package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.AgedmallReturnGoodsRecordMapper;
import org.linlinjava.litemall.db.domain.AgedmallReturnGoodsRecord;
import org.linlinjava.litemall.db.domain.AgedmallReturnGoodsRecordExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AgedmallReturnGoodsRecordService {
    @Resource
    private AgedmallReturnGoodsRecordMapper agedmallReturnGoodsRecordMapper;

    public List<AgedmallReturnGoodsRecord> getByOrderCode(String orderCode) {
        AgedmallReturnGoodsRecordExample example = new AgedmallReturnGoodsRecordExample();
        example.or().andDeletedEqualTo(false)
                .andOrderCodeEqualTo(orderCode);
        return agedmallReturnGoodsRecordMapper.selectByExampleWithBLOBs(example);
    }

    public void add(AgedmallReturnGoodsRecord agedmallReturnGoodsRecord) {
        agedmallReturnGoodsRecord.setAddTime(LocalDateTime.now());
        agedmallReturnGoodsRecord.setUpdateTime(LocalDateTime.now());
        agedmallReturnGoodsRecordMapper.insertSelective(agedmallReturnGoodsRecord);
    }

    public void deleteByOrderSnAndGoodsId(String orderSn, String goodsId) {
        AgedmallReturnGoodsRecordExample example = new AgedmallReturnGoodsRecordExample();
        example.or().andOrderCodeEqualTo(orderSn).andGoodsIdsEqualTo(goodsId).andDeletedEqualTo(false);
        AgedmallReturnGoodsRecord agedmallReturnGoodsRecord = agedmallReturnGoodsRecordMapper.selectOneByExample(example);
        if (null != agedmallReturnGoodsRecord) {
            agedmallReturnGoodsRecord.setDeleted(true);
            agedmallReturnGoodsRecordMapper.updateByPrimaryKey(agedmallReturnGoodsRecord);
        }
    }

    public AgedmallReturnGoodsRecord getByOrderCodeAndGoodsId(String orderCode, String goodsId) {
        AgedmallReturnGoodsRecordExample example = new AgedmallReturnGoodsRecordExample();
        example.or().andDeletedEqualTo(false)
                .andOrderCodeEqualTo(orderCode)
                .andGoodsIdsEqualTo(goodsId);
        return agedmallReturnGoodsRecordMapper.selectOneByExampleWithBLOBs(example);
    }

    public void upadte(AgedmallReturnGoodsRecord agedmallReturnGoodsRecord) {
        agedmallReturnGoodsRecord.setUpdateTime(LocalDateTime.now());
        agedmallReturnGoodsRecordMapper.updateByPrimaryKeySelective(agedmallReturnGoodsRecord);
    }
}
